﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using DiskUsage.CoreLib;
using DiskUsage.CoreLib.MessageBus;

namespace DiskUsage
{
    /// <summary>
    /// Interaction logic for DriveInfo.xaml
    /// </summary>
    public partial class DriveInfoControl : UserControl, IRefreshable
    {
        public DriveInfoControl()
        {
            this.InitializeComponent();
            PopulateDrives();
            MessageBus<RefreshMessage>.Instance.MessageRecieved += Instance_MessageRecieved;
        }

        void Instance_MessageRecieved(object sender, MessageBusEventArgs<RefreshMessage> e)
        {
            Refresh();
        }

        private void PopulateDrives()
        {
            var harddisk = new List<DriveInfoPresentation>();
            var removable = new List<DriveInfoPresentation>();
            Array.ForEach<DriveInfo>(DriveInfo.GetDrives(),
                a =>
                {
                    if (a.DriveType == DriveType.Fixed)
                        harddisk.Add(new DriveInfoPresentation(a));
                    else removable.Add(new DriveInfoPresentation(a));
                });

            m_harddisk.DataContext = harddisk;
            m_removable.DataContext = removable;
        }
        
        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            MessageBus<RefreshMessage>.Instance.SendMessage(this, new RefreshMessage());
        }

        public void Refresh()
        {
            PopulateDrives();
        }
    }
}